/******************************************************************************
 * This program is a 100% Java Email Server.
 ******************************************************************************
 * Copyright (c) 2001-2011, Eric Daugherty (http://www.ericdaugherty.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the copyright holder nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 * For current versions and more information, please visit:
 * http://javaemailserver.sf.net/
 *
 * or contact the authors at:
 * java@ericdaugherty.com
 * andreaskyrmegalos@hotmail.com
 *
 ******************************************************************************
 * This program is based on the CSRMail project written by Calvin Smith.
 * http://crsemail.sourceforge.net/
 ******************************************************************************
 *
 * $Rev$
 * $Date$
 *
 ******************************************************************************/

package com.ericdaugherty.mail.server.configuration;

//Java Imports
import javax.swing.JDialog;
import javax.swing.WindowConstants;

//Logging Imports
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A popup used to enter the keystore and/or kerberos 5 password(s) when no password exists
 * for the specified keystore/principal.
 * 
 * @author  Andreas Kyrmeglaos
 */
public class PasswordPopup extends JDialog {

   /** Logger */
    private static Log log = LogFactory.getLog( ConfigurationManager.class );
    
    private String labelName = "Please enter keystore password:";

    private Class caller;

   /** Creates new form BeanForm */
   public PasswordPopup() {
      this(null);
   }
   
   /** Creates new form BeanForm */
   public PasswordPopup(String labelName) {
      this(labelName, ConfigurationManager.class);
   }

   public PasswordPopup(String labelName, Class caller) {
      if (labelName!=null) this.labelName = labelName;
      this.caller = caller;
      initComponents();
      setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
      setAlwaysOnTop(true);
   }
   
   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      panel = new javax.swing.JPanel();
      passfield = new javax.swing.JPasswordField();
      okbutton = new javax.swing.JButton();
      cancelbutton = new javax.swing.JButton();
      label = new javax.swing.JLabel();

      setTitle("password entry");
      setMinimumSize(new java.awt.Dimension(240, 87));
      setName("password entry"); // NOI18N
      addWindowListener(new java.awt.event.WindowAdapter() {
         public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
         }
      });
      getContentPane().setLayout(new java.awt.FlowLayout());

      panel.setMaximumSize(new java.awt.Dimension(240, 87));
      panel.setMinimumSize(new java.awt.Dimension(240, 87));
      panel.setPreferredSize(new java.awt.Dimension(240, 87));
      panel.setLayout(new java.awt.GridBagLayout());

      passfield.setName("passfield"); // NOI18N
      passfield.setPreferredSize(new java.awt.Dimension(200, 27));
      passfield.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyPressed(java.awt.event.KeyEvent evt) {
            passfieldKeyPressed(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 1;
      gridBagConstraints.gridwidth = 2;
      panel.add(passfield, gridBagConstraints);

      okbutton.setText("OK");
      okbutton.setMaximumSize(new java.awt.Dimension(90, 23));
      okbutton.setMinimumSize(new java.awt.Dimension(90, 23));
      okbutton.setName("OK"); // NOI18N
      okbutton.setPreferredSize(new java.awt.Dimension(90, 23));
      okbutton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            okbuttonActionPerformed(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 2;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
      panel.add(okbutton, gridBagConstraints);

      cancelbutton.setText("Cancel");
      cancelbutton.setMaximumSize(new java.awt.Dimension(90, 23));
      cancelbutton.setMinimumSize(new java.awt.Dimension(90, 23));
      cancelbutton.setName("Cancel"); // NOI18N
      cancelbutton.setPreferredSize(new java.awt.Dimension(90, 23));
      cancelbutton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            cancelbuttonActionPerformed(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.gridy = 2;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      panel.add(cancelbutton, gridBagConstraints);

      label.setText(labelName);
      label.setName("keystorepass"); // NOI18N
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridwidth = 2;
      panel.add(label, gridBagConstraints);

      getContentPane().add(panel);
   }// </editor-fold>//GEN-END:initComponents

   private void okbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okbuttonActionPerformed
      exit();
   }//GEN-LAST:event_okbuttonActionPerformed

   private void cancelbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelbuttonActionPerformed
      exit();
   }//GEN-LAST:event_cancelbuttonActionPerformed

   private void passfieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passfieldKeyPressed
      if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
         exit();
      }
      else if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE) {
         ConfigurationManager.abort = true;
         exit();
      }
   }//GEN-LAST:event_passfieldKeyPressed

   private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
      exit();
   }//GEN-LAST:event_formWindowClosing
   
   private void exit() {
      if (caller.equals(ConfigurationManager.class)) {
         ConfigurationManager.addIdentityPassword("keystore", passfield.getPassword());
         ConfigurationManager.complete = true;
      }
      else {
         LoginCallbackHandlerGUI.setPassword(passfield.getPassword());
         LoginCallbackHandlerGUI.complete = true;
      }
      for (int i=passfield.getPassword().length-1;i>=0;i--) {
         passfield.getPassword()[i] = 0xFF;
      }
      setVisible(false);
      setEnabled(false);
      dispose();
      caller = null;
      labelName = null;
   }
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton cancelbutton;
   private javax.swing.JLabel label;
   private javax.swing.JButton okbutton;
   private javax.swing.JPanel panel;
   private transient javax.swing.JPasswordField passfield;
   // End of variables declaration//GEN-END:variables
   
}
